package com.student.controller;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.entity.Student;
import com.student.service.IStudentService;

@Controller
public class StudentController {

	@Autowired
	private IStudentService iss;
	
	@RequestMapping("save")
	@ResponseBody
	public String save(Student s) throws SQLException {
		if(1 == iss.save(s)) {
			return "success";
		}else {
			return "false";
		}
	}
	
	@RequestMapping("remove")
	@ResponseBody
	public String remove(Student s) throws SQLException {
		if(1 == iss.remove(s)) {
			return "success";
		}else {
			return "false";
		}
	}
	
	@RequestMapping("update")
	@ResponseBody
	public String update(Student s) throws SQLException {
		if(1 == iss.update(s)) {
			return "success";
		}else {
			return "false";
		}
	}
	
	@RequestMapping("queryAll")
	@ResponseBody
	public List<Student> queryAll() throws SQLException {
		return iss.queryAll();
	}
	
	@RequestMapping("queryByPage")
	@ResponseBody
	public List<Student> queryByPage(int page,int pageSize) throws SQLException{
		
		//1.!!!!设置开始页PageHelper 必须放在selectAll()之前
		PageHelper.startPage(page,pageSize);
		//2.得到所有元素
		List<Student> list = iss.queryAll();
		//3.得到分页后的内容
		PageInfo<Student> ss = new PageInfo<Student>(list);
		//4.返回分页后的内容
		return ss.getList();
		
	}
	
	@RequestMapping("queryById")
	@ResponseBody
	public Student queryById(String id) throws SQLException {
		return iss.queryById(id);
	}
	
}
